package util

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

// http请求
func HttpDo(method string, u string, values map[string]interface{}) ([]byte, error) {
	v := url.Values{}
	for key, val := range values {
		v.Set(key, fmt.Sprintf("%v", val))
	}
	body := ioutil.NopCloser(strings.NewReader(v.Encode()))       //把form数据编下码
	req, err := http.NewRequest(strings.ToUpper(method), u, body) // nolint
	if err != nil {
		return nil, err
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
	// req.Header.Set("Cookie", "name=anny")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	return ioutil.ReadAll(resp.Body)
}
